/*
 Copyright (C) 2007 - Michael Opitz
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
#include "config.h"
#include "ephy-gmail-extension.h"

#define LOG(str)			  (printf ("%s\n", (str)))

#include "gmail-ui.h"
#include "gmail.h"
#include "gmail-list.h"
#include "gmail-status-icon.h"

#include <malloc.h>
#include <string.h>
#include <glib/gstdio.h>

#define WINDOW_DATA_KEY 	"EphyGmailExtensionWindowData"
#define GMAIL_ICON_PATH 	\
	"/.gnome2/epiphany/extensions/data/gmailmanager/gmail.png"

#include <gmodule.h>
#include <gtk/gtk.h>
#include <epiphany/ephy-extension.h>
#include <epiphany/ephy-dialog.h>
#include <epiphany/ephy-window.h>
#include <epiphany/ephy-statusbar.h>
#include <epiphany/ephy-notebook.h>
#include <epiphany/ephy-shell.h>

#include <glade/glade.h>

#include <pthread.h>

#define EPHY_GMAIL_EXTENSION_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE(\
						 (object), \
						  EPHY_TYPE_GMAIL_EXTENSION, \
						  EphyGmailExtensionPrivate))

pthread_mutex_t login_mutex = PTHREAD_MUTEX_INITIALIZER;
struct _EphyGmailExtensionPrivate 
{
	/** 
	 * A pointer to the GmailList.
	 */
	GmailList *list;
	/** 
	 * A pointer to the list of opened urls.
	 */
	GList *urls;
};

typedef struct 
{
	/** 
	 * In each window we are storing the extension itself.
	 */
	EphyGmailExtension *extension;
	/** 
	 * In each window we are storing a EphyWindow.
	 */
	EphyWindow *window;
	
	/** 
	 * Each window may have a GmailUI dialog to edit/delete/create accounts.
	 */
	GmailUI *ui;
	/** 
	 * Each window contains a GmailStatusIcon.
	 */
	GmailStatusIcon *icon;
} WindowData;

enum 
{
	PROP_LIST = 1
};

static GObjectClass *parent_class = NULL;
static GType type = 0;

/** 
 * ephy_gmail_extension_open_ui:
 * 
 * @icon: a pointer to the GmailStatusIcon.
 * @data: a pointer to the WindowData.
 *
 * This callback is called from the GmailStatusIcon if the edit-window should be
 * opened. The function creates a new GmailUI dialog and displays it.
 *
 */
void 
ephy_gmail_extension_open_ui (GmailStatusIcon *icon, WindowData *data);

GType
ephy_gmail_extension_get_type ()
{
	return type;
}

static void 
ephy_gmail_extension_init (EphyGmailExtension *extension)
{
	extension->priv = EPHY_GMAIL_EXTENSION_GET_PRIVATE (extension);
	extension->priv->list = g_object_new (GMAIL_TYPE_LIST, NULL);
	extension->priv->urls = NULL;
	LOG ("EphyGmailExtension initialising");
}

static void 
ephy_gmail_extension_dispose (GObject *object)
{
	LOG ("EphyGmailExtension: dispose");
	GObjectClass *parent_class = g_type_class_peek_parent (
			EPHY_GMAIL_EXTENSION_GET_CLASS (EPHY_GMAIL_EXTENSION (
						        object)));
	EphyGmailExtension *self = EPHY_GMAIL_EXTENSION (object);
	g_object_unref (self->priv->list);
	G_OBJECT_CLASS (parent_class)->dispose (object);
}

static void
ephy_gmail_extension_finalize (GObject *object)
{
	EphyGmailExtension *ext = EPHY_GMAIL_EXTENSION (object);
	LOG ("EphyGmailExtension finalizing");
	g_list_foreach (ext->priv->urls, (GFunc) g_free, NULL);
	g_list_free (ext->priv->urls);
	G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
impl_attach_window (EphyExtension *ext, EphyWindow *window)
{
	EphyGmailExtension *xt = EPHY_GMAIL_EXTENSION (ext);
	WindowData *data;

	LOG ("EphyGmailExtension attach_window");

	data = g_new (WindowData, 1);
	g_object_set_data_full (G_OBJECT (window), WINDOW_DATA_KEY, data, 
				(GDestroyNotify) g_free);
	data->window = window;
	data->extension = xt;
	data->ui = NULL;

	data->icon = g_object_new (GMAIL_TYPE_STATUS_ICON, 
				   "gmail_list", data->extension->priv->list, 
				   "window", window, NULL);

	g_signal_connect (data->icon, "open-ui",
			  G_CALLBACK (ephy_gmail_extension_open_ui), data);
}

static void
impl_detach_window (EphyExtension *ext, EphyWindow *window)
{
	WindowData *data = (WindowData*) g_object_get_data (G_OBJECT (window), 
							    WINDOW_DATA_KEY);
	g_return_if_fail (data != NULL);
	LOG ("EphyGmailExtension detach_window");

	g_object_unref (data->icon);

	if (data->ui != NULL) {
		LOG ("unrefing data->ui");
		g_object_unref (G_OBJECT(data->ui));
	}
}

static void
impl_attach_tab (EphyExtension *ext,
		 EphyWindow *window,
		 EphyTab *tab)
{
	LOG ("attach_tab");
}

static void
impl_detach_tab (EphyExtension *ext,
		 EphyWindow *window,
		 EphyTab *tab)
{
	LOG("detach_tab");
}

static void
ephy_gmail_extension_set_property (GObject *obj,
				   guint prop_id,
				   const GValue *value,
				   GParamSpec *pspec)
{
}

static void
ephy_gmail_extension_get_property (GObject *obj,
				   guint prop_id,
				   GValue *value,
				   GParamSpec *pspec)
{
	EphyGmailExtension *ext = EPHY_GMAIL_EXTENSION (obj);
	switch (prop_id) 
	{
	case PROP_LIST:
		g_value_set_pointer (value, (gpointer) ext->priv->list);
		break;
	}
}

static void 
ephy_gmail_extension_iface_init (EphyExtensionIface *iface)
{
	iface->attach_window = impl_attach_window;
	iface->detach_window = impl_detach_window;
	iface->attach_tab = impl_attach_tab;
	iface->detach_tab = impl_detach_tab;
}

static void 
ephy_gmail_extension_class_init (EphyGmailExtensionClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	parent_class = g_type_class_peek_parent (klass);

	object_class->finalize = ephy_gmail_extension_finalize;
	object_class->dispose = ephy_gmail_extension_dispose;
	object_class->set_property = ephy_gmail_extension_set_property;
	object_class->get_property = ephy_gmail_extension_get_property;

	g_type_class_add_private (object_class, 
				 sizeof (EphyGmailExtensionPrivate));


	GParamSpec *spec;
	spec = g_param_spec_pointer ("list", "Account List", 
			"Account list which holds all GmailAccount-Instances",
			G_PARAM_READWRITE);
	g_object_class_install_property (object_class, PROP_LIST, spec);
}

GType
ephy_gmail_extension_register_type (GTypeModule *module)
{
	const GTypeInfo info = 
	{
		sizeof (EphyGmailExtensionClass),
		NULL, /* base_init */
		NULL, /* base_finalize */
		(GClassInitFunc) ephy_gmail_extension_class_init,
		NULL, /* class finalize */
		NULL, /* class_data*/
		sizeof (EphyGmailExtension),
		0, /* n_preallocs */
		(GInstanceInitFunc) ephy_gmail_extension_init
	};

	const GInterfaceInfo extension_info = 
	{
		(GInterfaceInitFunc) ephy_gmail_extension_iface_init,
		NULL,
		NULL
	};

	type = g_type_module_register_type (module,
					    G_TYPE_OBJECT,
					    "EphyGmailExtension",
					    &info, 0);
	g_type_module_add_interface (module,
				     type,
				     EPHY_TYPE_EXTENSION,
				     &extension_info);
	return type;
}

void 
ephy_gmail_extension_open_ui (GmailStatusIcon *icon, WindowData *data)
{
	if (data->ui == NULL) { 
		data->ui = g_object_new (GMAIL_UI_TYPE, "list",
					 data->extension->priv->list, NULL);
	}

	GMAIL_UI_GET_CLASS (data->ui)->show_dialog (data->ui);
}


